package com.notepad.adapter;

import java.util.ArrayList;
import java.util.List;

import com.notepad.R;
import com.notepad.db.entity.DBNoteEntity;
import com.notepad.domain.Notepad;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class HomeListAdapter extends BaseAdapter {
	private LayoutInflater mLayoutInflater;
	private ArrayList<Notepad> mList;

	public HomeListAdapter(Context context, ArrayList<Notepad> data) {
		mLayoutInflater = LayoutInflater.from(context);
        if(data == null || "".equals(data)){
            mList = new ArrayList<Notepad>();
        }
		mList = data;
	}

	public ArrayList<Notepad> getData() {
		return this.mList;
	}

	public void setData(ArrayList<Notepad> list) {
		this.mList = list;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		viewHolder holder = null;
		if (convertView == null) {
			holder = new viewHolder();
			convertView = mLayoutInflater
					.inflate(R.layout.home_list_item, null);
			holder.title = (TextView) convertView.findViewById(R.id.note_title);
			holder.time = (TextView) convertView.findViewById(R.id.note_time);
			holder.content = (TextView) convertView
					.findViewById(R.id.note_content);
	        convertView.setTag(holder);
		} else {
			holder = (viewHolder) convertView.getTag();
		}
		String cur_title = mList.get(position).getTitle();
		if(cur_title.length()>15){
			holder.title.setText(cur_title.substring(0, 14)+"…");;
		}else{
			holder.title.setText(cur_title);
		}

		holder.time.setText(mList.get(position).getTime());
		holder.content.setText(mList.get(position).getContent());

		return convertView;
	}

	private class viewHolder {
		private TextView title, time, content;
	}

}
